package cn.iphone.NPlayer.service.impl;

import cn.iphone.NPlayer.constants.RedisCourseTypeConstans;
import cn.iphone.NPlayer.domain.CourseType;
import cn.iphone.NPlayer.exception.GlobalExceptionCodeEnum;
import cn.iphone.NPlayer.mapper.CourseTypeMapper;
import cn.iphone.NPlayer.service.ICourseTypeService;
import cn.iphone.NPlayer.util.AssertUtil;
import cn.iphone.NPlayer.vo.CourseTypeCrumbsVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author ikun
 * @since 2022-11-22
 */
@Service
@Slf4j
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Override
    @Cacheable(cacheNames = RedisCourseTypeConstans.COURSE_TYPE_TREE_DATA_CHCHE_CACHENAMES,
            key = RedisCourseTypeConstans.COURSE_TYPE_TREE_DATA_CACHE_KEY)
    public List<CourseType> treeData() {
        log.info("数据库的数据");
        return getCourseTypes();
    }

    @Override
    public List<CourseTypeCrumbsVo> crumbs(Long courseTypeId) {
        // 1.参数校验
        // 2.业务校验
        CourseType courseType = selectById(courseTypeId);
        AssertUtil.isNotNull(courseType, GlobalExceptionCodeEnum.ERROR);
        //获取到自己的Id包括所有父级Id
        String path = courseType.getPath();
        AssertUtil.isNotNull(path, GlobalExceptionCodeEnum.ERROR);
        String[] split = path.split("\\.");
        List<CourseTypeCrumbsVo> courseTypeCrumbs = new ArrayList<>();
        Arrays.stream(split).map(Long::valueOf).collect(Collectors.toList()).forEach(courseTypeIdTmp -> {
            //1.根据当前类型Id，查询出自己的对象
            CourseType ownerProductType = selectById(courseTypeIdTmp);
            AssertUtil.isNotNull(ownerProductType,GlobalExceptionCodeEnum.ERROR);
            //2.根据自己的pid查询出所有自己同级对象
            Wrapper<CourseType> wrapper = new EntityWrapper<>();
            wrapper.eq("pid",ownerProductType.getPid());
            List<CourseType> otherProductTypes = selectList(wrapper);
            //3.创建要返回的面包屑对象，并把要展示的对象+自己同级所有集合数据放入到对象中
            CourseTypeCrumbsVo courseTypeCrumb = new CourseTypeCrumbsVo(ownerProductType,otherProductTypes);
            //4.因为我们的面包屑有很多组数据，所以我们根据层级关系放入到集合中
            courseTypeCrumbs.add(courseTypeCrumb);
    });
        return courseTypeCrumbs;
}

    private ArrayList<CourseType> getCourseTypes() {
        List<CourseType> courseTypes = selectList(null);
        Map<Long, CourseType> courseTypeMap = courseTypes.stream().collect(Collectors.toMap(CourseType::getId, courseType -> courseType));
        ArrayList<CourseType> treeData = new ArrayList<>();
        courseTypes.forEach(e -> {
            if (e.getPid() != null && e.getPid().intValue() == 0) {
                treeData.add(e);
            } else {
                CourseType courseType = courseTypeMap.get(e.getPid());
                if (courseType != null) {
                    courseType.getChildren().add(e);
                }
            }
        });
        return treeData;
    }

    @Override
    @Cacheable(cacheNames = RedisCourseTypeConstans.COURSE_TYPE_TREE_DATA_CHCHE_CACHENAMES,
            key = RedisCourseTypeConstans.COURSE_TYPE_TREE_DATA_CACHE_KEY)
    public boolean insert(CourseType entity) {
        return super.insert(entity);
    }

    @Override
    @Cacheable(cacheNames = RedisCourseTypeConstans.COURSE_TYPE_TREE_DATA_CHCHE_CACHENAMES,
            key = RedisCourseTypeConstans.COURSE_TYPE_TREE_DATA_CACHE_KEY)
    public boolean updateById(CourseType entity) {
        return super.updateById(entity);
    }

    @Override
    @Cacheable(cacheNames = RedisCourseTypeConstans.COURSE_TYPE_TREE_DATA_CHCHE_CACHENAMES,
            key = RedisCourseTypeConstans.COURSE_TYPE_TREE_DATA_CACHE_KEY)
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }
}
